(function(FormBase) {
    FormBase.Amount = FormBase.extend({
        singleConfig: {
            //需要区分多个金额表单 {formEleId : {} }
            defaultOption: {
                isInteger: false, //是否为整数，默认不是
                decimalDigits: 2, //大数位数，默认2位
                integerDigits: 8, //整数位数，默认8位
                minAmount: 10, //最小金额
                // maxAmount: 1000000000000000, //最大金额
                // "minAmountReplace":true, //超过最小阈值用最小阈值替换
                // "maxAmountReplace":true, //超过最大阈值用最大阈值替换
                errMsg: "ERR_Msg", //默认错误提示
                integerOnlyErrMsg: "INTEGER_ONLY_ERRMSG", //小数位默认错误提示
                decimalDigitsErrMsg: "DECIMAL_DIGITS_ERRMSG", //小数位默认错误提示
                integerDigitsErrMsg: "INTEGER_DIGITS_ERRMSG", //整数位默认错误提示
                minAmountErrMsg: "MINAMOUNT_ERRMSG", //最小值默认错误提示
                maxAmountErrMsg: "MAXAMOUNT_ERRMSG" //最大值默认错误提示
            }
        },
        msgObj: { // error msg
            ERR_Msg: "默认错误提示", //默认错误提示
            INTEGER_ONLY_ERRMSG: "小数位默认错误提示", //小数位默认错误提示
            DECIMAL_DIGITS_ERRMSG: "小数位默认错误提示", //小数位默认错误提示
            INTEGER_DIGITS_ERRMSG: "整数位默认错误提示", //整数位默认错误提示
            MINAMOUNT_ERRMSG: "最小值默认错误提示", //最小值默认错误提示
            MAXAMOUNT_ERRMSG: "最大值默认错误提示" //最大值默认错误提示
        },
        constructor: function(singleId, mainContent, formConfig) {
            this.inherit(singleId, mainContent, formConfig);
            //初始化 singleConfig
            !this.singleConfig[this.formEleId] && (this.singleConfig[this.formEleId] = {});
            this.singleConfig[this.formEleId] = $.extend({}, this.singleConfig.defaultOption, formConfig.singleConfig);
        },
        susFun: function() {},
        errFun: function() {},
        //针对IE8处理
        keypressActionIE: function(e) {
            var zz = /[0-9a-zA-Z.]*/,
                zz1 = /[0-9.]*/;
            var txt = this.getCharFromKeyCode(e);
            if (zz.test(txt)) {
                this.alreadyHandle = true;
                if (zz1.test(txt)) { //数字
                    this.newValue = this.oldValue + "" + txt;
                    var result = this.handleResult({
                        isPress: true
                    });
                    this.oldValue = this.newValue;
                    if (!result) {
                        if (this.limitInput) {
                            this.formEle.value = this.newValue;
                            return false;
                        }
                    }
                    return true;
                } else {
                    return false;
                }
            }
        },
        checkForm: function() {
            var singleConfig = this.getSingleConfig();
            var regNum = /[^\d.]/; //非数字
            var tempValue;
            //只包含数字和"."?
            var parseHdl = singleConfig.isInteger?parseInt:parseFloat;
            if(!this.newValue|| parseHdl(this.newValue) === 0){
                return {
                    status:false,
                    msg:'金额不能为空',
                    value:''
                }
            }
            // if (this.oldValue === this.newValue) {
            //     return {
            //         status: true,
            //         msg: null,
            //         value: this.newValue || ''
            //     };
            // }
           
            if (regNum.test(this.newValue)) {
                return {
                    status: false,
                    msg: this.getMsg(singleConfig.errMsg),
                    value: this.newValue.substring(0, this.newValue.length - 1)
                }
            }
            
            //是否要求为整数 && 含有小数点
            if (singleConfig.isInteger && this.newValue.indexOf(".") != -1) {
                console.log(this.newValue.substring(0, this.newValue.length - 1));
                return {
                    status: false,
                    msg: this.getMsg(singleConfig.integerOnlyErrMsg),
                    value: this.newValue.substring(0, this.newValue.length - 1)
                }
            }

            var str = this.newValue.split("."); //分成整数，小数
            tempValue = (str[0] ? parseInt(str[0]) + "" : "0") + (str.length == 2 ? (str[1] ? "." + str[1] : ".") : "");
            if (str.length > 2) { //多个小数点状况   
                return {
                    status: false,
                    msg: this.getMsg(singleConfig.errMsg),
                    value: this.newValue.substring(0, this.newValue.indexOf(".") + 1)
                }
            }
            //是否有整数位数限制 && 整数位数超限
            if (singleConfig.integerDigits && str[0] && str[0].length > singleConfig.integerDigits) {
                return {
                    status: false,
                    msg: this.getMsg(singleConfig.integerDigitsErrMsg),
                    value: this.newValue.substring(0, singleConfig.integerDigits)
                }
            }

            //是否有小数位数限制 && 小数位数超限
            if (singleConfig.decimalDigits && str[1] && str[1].length > singleConfig.decimalDigits) { //存在小数且小数点超过2位
                return {
                    status: false,
                    msg: this.getMsg(singleConfig.decimalDigitsErrMsg),
                    value: str[0]+'.'+str[1].substr(0, singleConfig.decimalDigits)
                }
            }
            //是否有最大阈值限制   && 超过最大阈值
            if (singleConfig.maxAmount && parseFloat(this.newValue) > singleConfig.maxAmount) {
                if (singleConfig.maxAmountReplace) {
                    return {
                        status: false,
                        msg: this.getMsg(singleConfig.maxAmountErrMsg),
                        value: singleConfig.maxAmount + ""
                    }
                } else { //不替换原数据
                    return {
                        status: false,
                        msg: this.getMsg(singleConfig.maxAmountErrMsg),
                        value: tempValue
                    }
                }
            }
            //是否有最小阈值限制   && 超过最小阈值
            if (singleConfig.minAmount && parseFloat(this.newValue) < singleConfig.minAmount) {
                if (singleConfig.minAmountReplace) {
                    return {
                        status: false,
                        msg: this.getMsg(singleConfig.minAmountErrMsg),
                        value: singleConfig.minAmount + ""
                    }
                } else { //不替换原数据
                    return {
                        status: false,
                        msg: this.getMsg(singleConfig.minAmountErrMsg),
                        value: tempValue
                    }
                }
            }
            return {
                status: true,
                msg: null,
                value: tempValue
            };
        }

    });
})(window.FormBase = window.FormBase || {})
